package com.example.demo.Controller;

import com.example.common.redis.RedisCache;
import com.example.common.utils.AjaxResult;
import com.example.demo.domain.City;
import com.example.demo.domain.CitySpotCountAndRating;
import com.example.demo.domain.CitySpotNum;
import com.example.demo.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 城市Controller
 */
@RestController
//@RequestMapping("/city")
@RequestMapping("/api/other/city")
public class CityController {
    //redis+type
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private CityService cityService;

    /**
     * 热门城市(x为省份 y为城市 z为热度值)
     *
     * @return
     */
    @GetMapping("/hot")
    public AjaxResult selectCityHot() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("CityHotInfo"));
        } catch (NullPointerException e) {
            HashMap<String, HashMap<String, Float>> CityHotInfoMap = cityService.selectCityHot();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("CityHotInfo", CityHotInfoMap, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", CityHotInfoMap);
        }
    }


    /**
     * 城市列表
     *
     * @return
     */
    @GetMapping("/cityList")
    public AjaxResult cityList() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("cityList"));
        } catch (NullPointerException e) {
            List<City> cities = cityService.selectAll();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("cityList", cities, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", cities);
        }
    }

    /**
     * 省份城市景区列表
     *
     * @return
     */
    @GetMapping("/provinceCityList")
    public AjaxResult provinceCityList() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("provinceCityList"));
        } catch (NullPointerException e) {
            HashMap<String, HashMap<String, List<String>>> provinceCitySpot = cityService.selectProvinceCitySpot();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("provinceCityList", provinceCitySpot, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", provinceCitySpot);
        }
    }


    /**
     * 该城市有几个景点
     *
     * @return
     */
    @GetMapping("/peopleCityList")
    public AjaxResult peopleCityList() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("peopleCityList"));
        } catch (NullPointerException e) {
            List<CitySpotNum> citySpotNums = cityService.selectCitySpotNum();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("peopleCityList", citySpotNums, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", citySpotNums);
        }
    }

    /**
     * 城市景点总数平均值
     *
     * @return
     */
    @GetMapping("/citySpotAvgRating")
    public AjaxResult citySpotAvgRating() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("citySpotAvgRating"));
        } catch (NullPointerException e) {
            List<CitySpotCountAndRating> citySpotCountAndRatings = cityService.selectCitySpotAvgRating();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("citySpotAvgRating", citySpotCountAndRatings, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", citySpotCountAndRatings);
        }
    }
}
